<?php

declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

use SixShop\Wangdian\WangdianFactory;
use SixShop\Wangdian\Exception\ApiException;
use SixShop\Wangdian\Exception\HttpException;

// Create client for sandbox environment
$client = WangdianFactory::createSandboxClient(
    sid: 'your-sid',
    appKey: 'your-app-key',
    appSecret: 'your-app-secret'
);

try {
    // Query shop information
    echo "Querying shop information...\n";
    $response = $client->basic()->queryShop('api_test');
    
    if ($response->isSuccess()) {
        $shopData = $response->getData();
        echo "Shop found: " . ($shopData['shop_name'] ?? 'Unknown') . "\n";
        echo "Response: " . $response->toJson() . "\n";
    } else {
        echo "Shop query failed: " . $response->getMessage() . "\n";
    }

    // Query warehouses
    echo "\nQuerying warehouses...\n";
    $response = $client->basic()->queryWarehouse();
    
    if ($response->isSuccess()) {
        $warehouses = $response->getData();
        echo "Found " . count($warehouses) . " warehouses\n";
        foreach ($warehouses as $warehouse) {
            echo "- " . ($warehouse['warehouse_name'] ?? 'Unknown') . " ({$warehouse['warehouse_no']})\n";
        }
    } else {
        echo "Warehouse query failed: " . $response->getMessage() . "\n";
    }
    
    // Query logistics providers
    echo "\nQuerying logistics providers...\n";
    $response = $client->basic()->queryLogistics();
    
    if ($response->isSuccess()) {
        $logistics = $response->getData();
        echo "Found " . count($logistics) . " logistics providers\n";
        foreach ($logistics as $provider) {
            echo "- " . ($provider['logistics_name'] ?? 'Unknown') . " (Type: {$provider['logistics_type']})\n";
        }
    } else {
        echo "Logistics query failed: " . $response->getMessage() . "\n";
    }

} catch (ApiException $e) {
    echo "API Error: " . $e->getMessage() . "\n";
    echo "API Code: " . $e->getApiCode() . "\n";
    if ($e->getResponseData()) {
        echo "Response Data: " . json_encode($e->getResponseData(), JSON_PRETTY_PRINT) . "\n";
    }
} catch (HttpException $e) {
    echo "HTTP Error: " . $e->getMessage() . "\n";
    echo "HTTP Status: " . $e->getHttpStatusCode() . "\n";
} catch (Exception $e) {
    echo "Unexpected Error: " . $e->getMessage() . "\n";
}